package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags = "01. 类别管理模块")
@Validated
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    // http://localhost:9180/categories/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/{id}/delete
    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "根据ID删除类别失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        categoryService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/{id}/update
    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("根据ID修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "根据ID修改类别失败，请提交合法的ID值！")
                                     @PathVariable Long id,
                                     @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【根据ID修改类别详情】请求，参数ID：{}，更新的数据，{}", id, categoryUpdateInfoParam);
        categoryService.updateInfoById(id, categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/{id}/enable
    @PostMapping("/{id:^\\d+$}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数ID：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/{id}/disable
    @PostMapping("/{id:^\\d+$}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数ID：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/{id}
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult getInfoById(@Range(min = 1, message = "根据ID查询类别详情，请提交合法的ID值！")
                                  @PathVariable Long id) {
        log.debug("开始处理【根据ID查询类别详情】请求，参数ID：{}", id);
        CategoryStandardVO categoryStandardById = categoryService.getCategoryStandardById(id);
        return JsonResult.ok(categoryStandardById);
    }

    // http://localhost:9180/categories/{id}/list
    @GetMapping("/{id:^\\d+$}/list")
    @ApiOperation("根据父级ID查询子级类别列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "父级类别ID", required = true, dataType = "long")
    public JsonResult list(@Range(min = 0, message = "根据父级ID查询子级类别列表失败，请提交合法的ID值！")
                           @PathVariable Long id) {
        List<CategoryListItemVO> categoryList = categoryService.getCategoryListByParentId(id);
        PageInfo<CategoryListItemVO> pageInfo = new PageInfo<>(categoryList);
        PageData<CategoryListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return JsonResult.ok(pageData);
    }

    // http://localhost:9180/categories/listTree
    @GetMapping("/listTree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        log.debug("开始处理【查询类别树】的请求，参数：无");
        List<CategoryTreeItemVO> categoryListTree = categoryService.listTree();
        return JsonResult.ok(categoryListTree);
    }
}
